#include "media_cpu.h"
#include "../../functions.h"

/**
 *
 * @param image image
 * @param filterImage
 * @param x position X
 * @param y position Y
 * @param width size X
 * @param height size Y
 * @param sizeX size window X
 * @param sizeY size window Y
 */
void media(uchar4* image, uchar4* filter, int x, int y, int width,int height,int sizeX,int sizeY){
	int RGB[3]={0};
	for(int j= y-(int)sizeY/2;j<=y+(int)sizeY/2;j++){
		for(int i=x-(int)sizeX/2;i<=x+(int)sizeX/2;i++){
			RGB[0]+=image[width*j+i].x;
			RGB[1]+=image[width*j+i].y;
			RGB[2]+=image[width*j+i].z;
		}
	}
	filter[width*y+x].x=RGB[0]/(sizeX*sizeY);
	filter[width*y+x].y=RGB[1]/(sizeX*sizeY);
	filter[width*y+x].z=RGB[2]/(sizeX*sizeY);
}

/**
 * makes the media filter
 * @param img
 * @param width size X
 * @param height size Y
 * @param kernel_sizeX size window X
 * @param kernel_sizeY size window Y
 * @return
 */
uchar4* media_cpu(uchar4* img,int width,int height,int kernel_sizeX, int kernel_sizeY){
	uchar4* filter = copy(img,width,height);

	int edgeX = kernel_sizeX/2;
	int edgeY = kernel_sizeY/2;
	for(int y=0;y<height;y++){
		for(int x=0;x<width;x++){
			if(x>=edgeX && x<height-edgeX && y>=edgeY && y<height-edgeY)
				media(img,filter,x,y,width,height,kernel_sizeX,kernel_sizeY);
			else
				filter[y*width+x]=img[y*width+x];
		}
	}
	return filter;
}
